import Vue from 'vue'
import Vuex from 'vuex'
import { updatePaper as updatePaperAPI, deletePaper as deletePaperAPI } from '@/api/paper'

Vue.use(Vuex)

export default {
  state: {
    // 选中的id
    checkedPaper: {
      paperId: 1
    },
    // 所有的试卷
    papers: [],
    loading: false,
    // paperQuestions: [
    //   {
    //     paperQuestionId: 1,
    //     paperId: 1,
    //     question: {
    //       questionId: 101,
    //       bankId: 1,
    //       content: "题目内容1",
    //       options: "值1,值2,值3,值4", // 选项格式调整
    //       correctAnswer: "值1", // 正确答案调整
    //       parse: "这是题目1的解析",
    //       type: 0, // 单选
    //       difficulty: 5,
    //       isDel: 0
    //     },
    //     order: 1,
    //     score: 5,
    //     isDel: 0
    //   },
    //   {
    //     paperQuestionId: 2,
    //     paperId: 1,
    //     question: {
    //       questionId: 102,
    //       bankId: 2,
    //       content: "题目内容2",
    //       options: "值A,值B,值C,值D", // 选项格式调整
    //       correctAnswer: "值B", // 正确答案调整
    //       parse: "这是题目2的解析",
    //       type: 1, // 多选
    //       difficulty: 7,
    //       isDel: 0
    //     },
    //     order: 2,
    //     score: 5,
    //     isDel: 0
    //   },
    //   {
    //     paperQuestionId: 3,
    //     paperId: 1,
    //     question: {
    //       questionId: 103,
    //       bankId: 3,
    //       content: "填空题内容：请填写正确答案。",
    //       correctAnswer: "正确答案", // 填空题答案
    //       parse: "这是填空题的解析",
    //       type: 2, // 填空
    //       difficulty: 4,
    //       isDel: 0
    //     },
    //     order: 3,
    //     score: 5,
    //     isDel: 0
    //   },
    //   {
    //     paperQuestionId: 4,
    //     paperId: 1,
    //     question: {
    //       questionId: 104,
    //       bankId: 4,
    //       content: "判断题内容：这个陈述是否正确？",
    //       options: "正确,错误", // 判断题选项
    //       correctAnswer: "正确", // 判断题答案
    //       parse: "这是判断题的解析",
    //       type: 3, // 判断
    //       difficulty: 3,
    //       isDel: 0
    //     },
    //     order: 4,
    //     score: 5,
    //     isDel: 0
    //   },
    //   {
    //     paperQuestionId: 5,
    //     paperId: 1,
    //     question: {
    //       questionId: 105,
    //       bankId: 5,
    //       content: "简答题内容：请简要回答以下问题。",
    //       correctAnswer: "简答题的正确答案", // 简答题答案
    //       parse: "这是简答题的解析",
    //       type: 4, // 简答
    //       difficulty: 6,
    //       isDel: 0
    //     },
    //     order: 5,
    //     score: 10,
    //     isDel: 0
    //   }
    // ]

    // 生成试卷id
    generatePaperId: 0, // 生成的试卷id
    paperQuestions: [], // 生成的试卷题目
    generateModelStatus: false, // 生成试卷的状态
    listParam: {
      pageNum: 1,
      pageSize: 10,
      paperName: ""
    },
    total: 0, // 试卷总数
  },
  getters: {
    getCheckedPaper: state => state.checkedPaper,
    getPapers: state => state.papers,
    getLoading: state => state.loading,
    getPaperQuestions: state => state.paperQuestions,
    getGeneratePaperId: state => state.generatePaperId,
    getGenerateModelStatus: state => state.generateModelStatus,
    getListParam: state => state.listParam,
    getTotal: state => state.total
  },
  mutations: {
    setCheckedPaper(state, paper) {
      state.checkedPaper = paper
    },
    setPapers(state, value) {
      state.papers = value
    },
    setLoading(state, value) {
      state.loading = value
    },
    setPaperQuestions(state, value) {
      state.paperQuestions = value
    },
    setGeneratePaperId(state, value) {
      state.generatePaperId = value
    },
    setGenerateModelStatus(state, value) {
      state.generateModelStatus = value
    },
    setListParam(state, value) {
      state.listParam = value
    },
    setTotal(state, value) {
      state.total = value
    },
    // 更新本地试卷数据
    updatePaperInList(state, updatedPaper) {
      const index = state.papers.findIndex(p => p.paperId === updatedPaper.paperId);
      if (index !== -1) {
        state.papers[index] = {
          ...state.papers[index],
          ...updatedPaper,
          updatedAt: new Date().toISOString()
        };
      }
    },
    // 从列表中删除试卷
    removePaperFromList(state, paperId) {
      state.papers = state.papers.filter(p => p.paperId !== paperId);
    }
  },
  actions: {
    // 更新试卷
    async updatePaper({ commit }, paperData) {
      try {
        await updatePaperAPI(paperData);
        commit('updatePaperInList', paperData);
        return Promise.resolve();
      } catch (error) {
        return Promise.reject(error);
      }
    },
    
    // 删除试卷
    async deletePaper({ commit }, paperId) {
      try {
        await deletePaperAPI({ paperId });
        commit('removePaperFromList', paperId);
        return Promise.resolve();
      } catch (error) {
        return Promise.reject(error);
      }
    }
  }
}
